<?php

namespace Admin\Controller;

use Think\Controller;

class UserController extends Controller
{
    var $user;

    function __construct()
    {
        parent::__construct();
        $this->user = M('User');
    }

    function register()
    {
        if (IS_POST) {
            $User = D('User');
            $b = $User->create();

            if (!$b) {
                $this->error($User->getError());
            } else {
                $User->add();
                $this->success('注册成功', U('login'));
            }


        } else {
            $this->display();
        }
    }

    function login()
    {
        if (IS_POST) {
            $b = $this->check_verify(I('post.code'));
            if (!$b) {
                $this->error('验证码错误');
            }

            $data['username'] = trim(I('post.username'));
            $data['password'] = set_password(I("post.password"));//这里并没有引入模型，所以必须手动md5加密！！！
            $user = $this->user->where($data)->find();

            if (!$user) {
                $this->error('用户名或密码错误');

            }
            if(I('post.rem')){


            }            else {
                $_SESSION['user'] = $user;


                $this->success('成功登陆', U('Index/index'));
            }


        } else {
            $this->display();

        }


    }

    function verify()
    {
        $config = array(
            'fontSize' => 25,    // 验证码字体大小
            'length' => 4,     // 验证码位数
            'useNoise' => 0, // 关闭验证码杂点
            'codeSet' => '0123456789',
            'useCurve' => 0
        );
        $Verify = new \Think\Verify($config);
        $Verify->entry();

    }

    function check_verify($code, $id = '')
    {
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }

    function logout()
    {
        $_SESSION['user'] = array();
        session_destroy();
        $this->success('退出登陆', U('login'));
    }
}